﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using DAO;

namespace Banking.Nhân_viên
{
    public partial class frmChiTietTKTK : Form
    {
        TaiKhoanTietKiem tk = new TaiKhoanTietKiem();
        TypeDemo _type;
        int _form;
        public frmChiTietTKTK(int maKH,int maTKTK,int form,TypeDemo type)
        {
            InitializeComponent();
            _type = type;
            _form = form;
            if (form == 0)
            {
                tk.MaTKChinh = TaiKhoanChinhDA.GetByMaKH(maKH);
                btnTaoTK.Visible = true;
                txtTienGoc.ReadOnly = false;
            }
            else
            {
                tk = TaiKhoanTietKiemDA.GetByID(maTKTK);
                btnRutTien.Visible = true;
                btnDaoHan.Visible = true;
                btnClear.Visible = true;
            }
        }

        private void frmChiTietTKTK_Load(object sender, EventArgs e)
        {
            txtMaTK.Text = tk.MaTKChinh.ToString();
            cmbKyHan.DataSource = LoaiKyHanLSDA.DSKyHan();
            cmbKyHan.DisplayMember = "TenLoai";
            cmbKyHan.ValueMember = "MaLoaiKHLS";
            cmbKyHan.SelectedIndex = -1;
            if (_form == 1)
            {
                txtTienGoc.Text = tk.SoTien.ToString();
                TimeSpan t1 = tk.NgayDaoHan - DateTime.Today;
                if (t1.Days < 1)
                {
                    txtTienLai.Text = (tk.SoTien * tk.ChiTietLaiSuat.MucLS / 12).ToString();
                    btnDaoHan.Enabled = true;
                }
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            cmbKyHan.SelectedIndex = -1;
        }

        private void btnDaoHan_Click(object sender, EventArgs e)
        {
            if (cmbKyHan.SelectedIndex >= 0)
            {
                int month=0;
                string[] str = cmbKyHan.Text.ToString().Split(' ');
                if (int.TryParse(str[0], out month))
                {
                    tk.NgayDaoHan = DateTime.Today.AddDays(month * 30);
                    tk.NgayGui = DateTime.Today;
                }
                else
                {
                    tk.NgayDaoHan = tk.NgayGui = DateTime.Today;
                }
                tk.SoTien = float.Parse(txtTienGoc.Text) + float.Parse(txtTienLai.Text);
                if (TaiKhoanTietKiemDA.DaoHan(tk, _type.typeDemo))
                    MessageBox.Show("Success");
            }
        }

        private void btnTaoTK_Click(object sender, EventArgs e)
        {
            if (cmbKyHan.SelectedIndex >= 0)
            {
                int month = 0;
                string[] str = cmbKyHan.Text.ToString().Split(' ');
                if (int.TryParse(str[0], out month))
                {
                    tk.NgayDaoHan = DateTime.Today.AddDays(month * 30);
                    tk.NgayGui = DateTime.Today;
                    tk.MaLoaiTK = 1;//co ky han
                }
                else
                {
                    tk.MaLoaiTK = 2;//khong ky han
                    tk.NgayDaoHan = tk.NgayGui = DateTime.Today;
                }
                tk.MaChiTietLS = int.Parse(cmbKyHan.SelectedValue.ToString());
                tk.SoTien = float.Parse(txtTienGoc.Text);
                if (TaiKhoanTietKiemDA.Insert(tk, _type.typeDemo))
                    MessageBox.Show("Success");
            }

        }

        private void btnRutTien_Click(object sender, EventArgs e)
        {
            if(TaiKhoanTietKiemDA.RutSoTK(tk,_type.typeDemo))
                MessageBox.Show("Success");
        }
    }
}
